package com.atgm.mall.gmmallseckill.scheduled;

import com.atgm.mall.gmmallseckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SecKillSkuScheduled {

    @Autowired
    SeckillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    //秒杀商品上架功能的锁
    private final String upload_lock = "seckill:upload:lock";

    @Scheduled(cron = "* * 3 * * ?")
    public void uploadSecKillSkuLatest3Days() {
        log.info("上架秒杀商品");
        //加入分布式锁，
        RLock lock = redissonClient.getLock(upload_lock);
        try {
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadSecKillSkuLatest3Days();
        }finally {
            lock.unlock();  //解锁
        }
    }

}
